package com.platform.controller.shop;

import com.platform.dao.shop.UserRecordDao;
import com.platform.entity.shop.UserEntity;
import com.platform.entity.shop.UserRecordEntity;
import com.platform.entity.shop.UserRecordRefundCash;
import com.platform.service.shop.UserRecordService;
import com.platform.utils.Base64;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import com.platform.utils.StringUtils;
import com.platform.utils.excel.ExcelExport;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Controller
 *
 * @author zhouhaisheng
 * @email
 * @date
 */
@RestController
@RequestMapping("userRecord")
public class UserRecordController {
    @Autowired
    private UserRecordService userRecordService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("userRecord:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<UserRecordEntity> userRecordList = userRecordService.queryList(query);

        int total = userRecordService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(userRecordList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 导出会员
     */
    @RequestMapping("/export")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) {

        List<UserRecordEntity> userList = userRecordService.queryList(params);

        String exportFileName = "会员提现申请列表";
        if(params.get("cTime") != null){
            exportFileName += "_"+params.get("cTime");
        }
        ExcelExport ee = new ExcelExport(exportFileName);

        String[] header = new String[]{"用户id","会员名称", "姓名", "证件号码", "银行卡号", "银行卡开户行", "手机号", "提现金额", "实际提现金额", "实际发放金额（元）", "发放金额（元）", "可用余额", "提现申请时间"};

        List<Map<String, Object>> list = new ArrayList<>();

        if (userList != null && userList.size() != 0) {
            for (UserRecordEntity userEntity : userList) {
                LinkedHashMap<String, Object> map = new LinkedHashMap<>();
                map.put("userId", userEntity.getUserId());
                map.put("userName", userEntity.getUserName());
                map.put("realName", userEntity.getRealName());
                map.put("idCard", userEntity.getIdCard());
                map.put("bankCard", userEntity.getBankCard());
                map.put("openBank", "");
                map.put("mobile", userEntity.getMobile());
                map.put("price", userEntity.getPrice());// 提现金额（用户输入的）

//                BigDecimal out = new BigDecimal(userEntity.getPrice()).multiply(new BigDecimal("0.93")).setScale(0,   BigDecimal.ROUND_HALF_UP);
//                BigDecimal realOut = new BigDecimal(userEntity.getPrice()).divide(new BigDecimal("1.07"), 0, BigDecimal.ROUND_HALF_UP);
//                BigDecimal realPrice = out.multiply(new BigDecimal("1.07")).setScale(0,   BigDecimal.ROUND_HALF_UP);

                map.put("realPrice", userEntity.getRealPrice());//=实际提现金额= 发放金额 * 1.07
                map.put("realOut", userEntity.getRealOut());//=实际发放金额(正常财务计算的) = 提现金额 / 1.07
                map.put("out", userEntity.getOut());//=发放金额(给到财务公司的，也是给用户看的) = 提现金额 * （1 - 0.07)

                map.put("amount", userEntity.getAmount());
                map.put("ctime", userEntity.getCtime());

                list.add(map);
            }
        }

        ee.addSheetByMap("会员提现申请", list, header);
        ee.export(response);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{reportId}")
    public R info(@PathVariable("reportId") Integer reportId) {
        Map<String, Object> params = new HashMap<>();
        params.put("reportId", reportId);
        List<UserRecordEntity> userList = userRecordService.queryList(params);
        return R.ok().put("userRecord", userList.get(0));
    }



    /**
     * 驳回
     */
    @RequestMapping("/refundCash")
    public R refundCash(@RequestBody UserRecordRefundCash userRecordRefundCash) {

        userRecordService.refundCash(userRecordRefundCash);

        return R.ok();
    }

}
